package univ.uchiha.esiag.gon.accesser;


import univ.uchiha.esiag.gon.operation.Operation;
import univ.uchiha.esiag.gon.topic.Topic;

public class DataReader implements DataAccess, DataReaderListener{
	private DataReactor dataReactor;
	private Topic topic;
	public DataReader(Topic topic, DataReactor dataReactor) {
		this.dataReactor = dataReactor;
		this.dataReactor.setListener(this);
		this.topic = topic;
	}
	public void notification() {
		Operation obj = dataReactor.readData();
		operate(obj);
	}
	private void operate(Operation operation){
		if(operation.getTarget().equals(Operation.CACHE)){
			if(operation.getType() == Operation.CREATE){
				topic.createCache(operation);
			}else if(operation.getType() == Operation.DELETE){
				topic.deleteCache(operation);
			}
			else if(operation.getType() == Operation.UPDATE){
				topic.updateCache(operation);
			}
			else if(operation.getType() == Operation.NOTIFY){
				topic.notifyAuthers();
			}
		}else if(operation.getTarget().equals(Operation.TOPIC)){
			if(operation.getType() == Operation.CREATE){
				topic.createTopic(operation);
			}else if(operation.getType() == Operation.DELETE){
				topic.deleteTopic(operation);
			}
		}
	}
	public String getTopicName() {
		return topic.getName();
	}
}
